package com.lhy.service;

import cn.hutool.core.collection.CollUtil;
import com.lhy.pojo.Gas;
import com.lhy.pojo.concentration;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EchartsService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private GridFsTemplate gridFsTemplate;

    //气体浓度表
    public Map<String,Object> selectAll(){
        List<concentration> list = mongoTemplate.findAll(concentration.class,"concentration");
        Map<String,Object> result =new HashMap<>();

        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            Query queryCO = new Query(Criteria.where("CO").gte(0.0024));
            concentration dangerCO=mongoTemplate.findOne(queryCO,concentration.class,"concentration");
            Query queryH2S = new Query(Criteria.where("H2S").gte(0.00066));
            concentration dangerH2S=mongoTemplate.findOne(queryH2S,concentration.class,"concentration");
            Query queryNO2 = new Query(Criteria.where("NO2").gte(0.00025));
            concentration dangerNO2=mongoTemplate.findOne(queryNO2,concentration.class,"concentration");
            Query querySO2 = new Query(Criteria.where("SO2").gte(0.0005));
            concentration dangerSO2=mongoTemplate.findOne(querySO2,concentration.class,"concentration");
            Query queryNH3 = new Query(Criteria.where("NH3").gte(0.0005));
            concentration dangerNH3=mongoTemplate.findOne(queryNH3,concentration.class,"concentration");

            result.put("code","0");
            if(dangerCO!=null || dangerH2S!=null || dangerNO2!=null || dangerSO2!=null || dangerNH3!=null) result.put("danger","1");

            result.put("count",list.size());
            result.put("data",list);
        }
        return result;
    }

    public Map<String,Object> bar(){
        List<com.lhy.pojo.User> list = mongoTemplate.findAll(com.lhy.pojo.User.class,"user");
        Map<String,Object> result =new HashMap<>();
        int q0=0;
        int q1=0;
        int q2=0;
        for (com.lhy.pojo.User user : list){
            Integer A=user.getAuthority();
            if(A==0)
            {
                q0++;
                continue;
            }
            if(A==1) {
                q1++;
                continue;
            }
            if(A==2) {
                q2++;
                continue;
            }
        }
        ArrayList<Integer> list1 = CollUtil.newArrayList(q0, q1, q2);
        result.put("code","0");
        result.put("count",list.size());
        result.put("data",list1);
        return result;
    }

    public Map<String,Object> miner(){
        List<com.lhy.pojo.Miner> list = mongoTemplate.findAll(com.lhy.pojo.Miner.class,"miner");
        Map<String,Object> result =new HashMap<>();
        int q0=0;
        int q1=0;
        int q2=0;
        int q3=0;
        int q4=0;
        int q5=0;
        int q6=0;
        int q7=0;
        int q8=0;
        int q9=0;
        int q10=0;
        for (com.lhy.pojo.Miner miner : list){
            String A=miner.getPosition();
            switch(A){
                case "采矿工": q0++; break;
                case "掘进工": q1++; break;
                case "电工": q2++; break;
                case "瓦斯检察员": q3++; break;
                case "测风工": q4++; break;
                case "防尘工": q5++; break;
                case "通风工": q6++; break;
                case "放炮员": q7++; break;
                case "安全员": q8++; break;
                case "推车工": q9++; break;
                case "其他": q10++; break;
                default: break;
            }
        }
        ArrayList<Integer> list1 = CollUtil.newArrayList(q0, q1, q2,q3,q4,q5,q6,q7,q8,q9,q10);
        result.put("code","0");
        result.put("count",list.size());
        result.put("data",list1);
        return result;
    }

    public Map<String,Object> devices(){
        List<com.lhy.pojo.Devices> list = mongoTemplate.findAll(com.lhy.pojo.Devices.class,"devices");
        Map<String,Object> result =new HashMap<>();
        int q0=0;
        int q1=0;
        int q2=0;
        for (com.lhy.pojo.Devices devices : list){
            String A=devices.getDeviceStatus();
            switch(A){
                case "使用中": q0++; break;
                case "待使用": q1++; break;
                case "维修中": q2++; break;
                default: break;
            }
        }
        ArrayList<Integer> list1 = CollUtil.newArrayList(q0, q1, q2);
        result.put("code","0");
        result.put("count",list.size());
        result.put("data",list1);
        return result;
    }

    //瓦斯含量表
    public Map<String,Object> gas(){
        List<com.lhy.pojo.Gas> list = mongoTemplate.findAll(com.lhy.pojo.Gas.class,"gas");
        Map<String,Object> result =new HashMap<>();
        if(list==null || list.isEmpty()){
            result.put("code","400");
            result.put("message","没有相关信息");
        }else{
            Query query = new Query(Criteria.where("concentration").gte(5));
            Gas danger=mongoTemplate.findOne(query,Gas.class,"gas");
            result.put("code","0");
            if(danger!=null) result.put("danger","1");
            result.put("count",list.size());
            result.put("data",list);
        }
        return result;
    }

    //警报音
    public GridFSFile findOne(){
        GridFSFile result = gridFsTemplate.findOne(new Query(Criteria.where("filename").is("0412.mp3")));
        return result;
    }

}
